package cn.itrip.trade.service.impl;

import cn.itrip.beans.pojo.ItripHotelOrder;
import cn.itrip.beans.pojo.ItripTradeEnds;
import cn.itrip.dao.hotelorder.ItripHotelOrderMapper;
import cn.itrip.dao.hoteltempstore.ItripHotelTempStoreMapper;
import cn.itrip.trade.service.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private ItripHotelOrderMapper hotelOrderMapper;
    @Resource
    private ItripHotelTempStoreMapper itripHotelTempStoreMapper;
    @Override
    public ItripHotelOrder hotelOrder(String orderNo) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("orderNo",orderNo);
        List<ItripHotelOrder> orderList = hotelOrderMapper.getItripHotelOrderListByMap(map);
        if(orderList.size()>0){
            return orderList.get(0);
        }else {
            return null;
        }
    }

    @Override
    public void paySuccess(String orderNo, int payType, String tradeNo) throws Exception {
        ItripHotelOrder order =this.hotelOrder(orderNo);
        order.setPayType(payType);
        order.setOrderStatus(2);
        order.setTradeNo(tradeNo);
        this.hotelOrderMapper.updateItripHotelOrder(order);
        //减库存
        Map<String,Object> room = new HashMap<String, Object>();
        room.put("startTime",order.getCheckInDate());
        room.put("endTime",order.getCheckOutDate());
        room.put("count",order.getCount());
        room.put("roomId",order.getRoomId());
        itripHotelTempStoreMapper.updateRoomStore(room);
    }

    @Override
    public void payFailed(String orderNo, int payType, String tradeNo) throws Exception {
        ItripHotelOrder order =this.hotelOrder(orderNo);
        order.setPayType(payType);
        order.setOrderStatus(1);
        order.setTradeNo(tradeNo);
        this.hotelOrderMapper.updateItripHotelOrder(order);
    }
}
